//
//  ViewController.swift
//  Test
//
//  Created by Louis Liu on 24/01/2018.
//  Copyright © 2018 Louis Liu. All rights reserved.
//

import UIKit
import JLRoutes
import SFFundation
import Alamofire
import SwiftyJSON

class TabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        initTabBarItems()
    }
    
    func initTabBarItems(){
        
        let path = Bundle.main.path(forResource: "Config", ofType: "plist")!
        let config = NSDictionary(contentsOfFile: path)
        
        guard let tabBarConfig = config?["TabBar"], tabBarConfig is Array<Any> else { return }
        
        let tabBarData = tabBarConfig as! Array<Any>
        var controllers = [UIViewController]()
        
        for i in 0..<tabBarData.count {
            let data = tabBarData[i]
            let dict = data as! Dictionary<String, String>
            let storyBoardName = dict["StoryBoard"]
            let viewName       = dict["ViewController"]
            let itemName       = dict["ItemName"]
            let itemIcon       = dict["ItemIcon"]
            var viewController:UIViewController?
            
            if(storyBoardName == nil || storyBoardName == ""){
                
            }else{
                let storyBoard = UIStoryboard(name: storyBoardName!, bundle: nil)
                viewController = storyBoard.instantiateViewController(withIdentifier: viewName!)
            }
            
            if let controller = viewController {
                
                let tabBarItem = UITabBarItem(title: itemName, image: nil, tag: i)
                controller.tabBarItem = tabBarItem
                controllers.append(controller)
            }
        }
        self.viewControllers = controllers
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
